package com.burgess.net.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 创建人    BurgessLee
 * 创建时间   2020/5/28
 * 描述
 */
public class QueueReceiver {

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection conn = connFactory.createConnection();
        conn.start();
        Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        try {
            Queue queue = session.createQueue("my-queue");
            MessageConsumer consumer = session.createConsumer(queue);

            for (int i = 0; i < 3; i++) {
                Message message = consumer.receive();
                String jmsType = message.getJMSType();
                System.out.println("消息类型:"+jmsType);
                TextMessage tx=(TextMessage)message;
                session.commit();
                System.out.println("收到消息:"+tx.getText());
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            session.close();
            conn.close();
        }
    }

}
